//
//  RbTextClearView.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/9/22.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "RbTextClearView.h"


@interface RbTextClearView ()

@property (nonatomic,strong) UIButton *clearBtn;


@end

@implementation RbTextClearView

+ (instancetype)clearTypeLog{
    return [RbTextClearView initWithType:CLEAR_LOG];
}


+ (instancetype)clearTypeSettings{
    return [RbTextClearView initWithType:CLEAR_SETTINGS];
}


+ (instancetype)initWithType:(CLEAR_TYPE)type{
    RbTextClearView *view  = [[self alloc] init];
    view.clearType = type;
    if (view.clearType == CLEAR_LOG) {
        [view.clearBtn setBackgroundImage:[UIImage imageNamed:@"clear_log"] forState:UIControlStateNormal];
    }else{
        [view.clearBtn setBackgroundImage:[UIImage imageNamed:@"clear_settings"] forState:UIControlStateNormal];
    }
    return view;
}



- (instancetype)init{
    self = [super init];
    if (self) {
        [self setupSubViews];
        [self updateConstraintsIfNeeded];
        
        UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setupSubViews{
    self.clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.clearBtn];
    self.clearBtn.userInteractionEnabled = NO;
//    [self.clearBtn addTarget:self action:@selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateConstraints{
    [super updateConstraints];
    [self.clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-15);
        make.width.height.mas_equalTo(IMAGE_W);
    }];
    
}

- (void)clearText:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(rbTextClearCurrentText:)]) {
        [self.delegate rbTextClearCurrentText:self];
    }
}

- (void)tapView{
    if ([self.delegate respondsToSelector:@selector(rbTextClearCurrentText:)]) {
        [self.delegate rbTextClearCurrentText:self];
    }
}
@end
